package com.my.hp_hr.contorller;


import com.my.hp_hr.entity.Position;
import com.my.hp_hr.service.PositionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/addPosition")
public class AddPosition extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("utf-8");
            PositionService positionService1 = new PositionService();
            List<Position> list = positionService1.listPosition();
            int num=0;
            for (int i = 0; i < list.size(); i++) {
                Integer position_number = list.get(list.size()-1).getPosition_number();
                num=position_number;
                break;
            }
            int positionNum = num+1;
            String name = req.getParameter("name");
            String level = req.getParameter("level");
            String notes = req.getParameter("notes");
            Position position = new Position(0,positionNum,name,level,notes);
            PositionService positionService = new PositionService();
            positionService.addPosition(position);
            req.getRequestDispatcher("pagePosition").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
